﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class ComicStatusController
    {
        public IList<ComicStatus> GetAllComicStatus()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<ComicStatus>().ToList();
            }
        }

        public IList<String> GetAllComicStatusStatus()
        {
            return GetAllComicStatus().Select(comicStatus => comicStatus.Status).ToList();
        }

        public string GetComicStatusStatus(ComicStatus comicStatus)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<ComicStatus>().ToList().
                    Where(c => c.Id == comicStatus.Id).
                    Select(c => c.Status).
                    Single();
            }
        }

        public int AddComicStatus(ComicStatus comicStatus)
        {
            try
            {
                GetAllComicStatus()
                    .Where(c => c.Status == comicStatus.Status)
                    .Select(c => c)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(comicStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditComicStatus(ComicStatus newComicStatus, String oldStatus)
        {
            try
            {
                var checkComicStatus = GetAllComicStatus()
                    .Where(c => c.Status == newComicStatus.Status)
                    .Select(c => c)
                    .Single();

                if (checkComicStatus.Status == oldStatus)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newComicStatus);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newComicStatus);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteComicStatus(ComicStatus comicStatus)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(comicStatus);
                    transaction.Commit();
                }
            }
        }
    }
}
